package com.wandian.texas.command.texas;

import com.wandian.command.Cmd;
import com.wandian.command.UserCommand;
import com.wandian.texas.BaseCommandObject;
import com.wandian.texas.GamePool;
import com.wandian.texas.RoomMgr;
import com.wandian.texas.UserMgr;
import com.wandian.texas.db.DaoManager;
import com.wandian.texas.entity.UserInfoEntity;
import com.wandian.texas.protocol.Protocol;
import com.wandian.util.Log;
import com.wandian.xml.RoomConfigItem;

/**
 * @author long
 * @Create 2012-10-27 下午12:09:09
 * @QQ 108232706
 * @version 1.0
 * @description GameRoomCmd
 */
@Cmd(code = Protocol.GameRoomCmd, desc = "")
public class GameRoomCmd extends UserCommand {
	@Override
	public void execute(BaseCommandObject cmdObject){
		super.execute(cmdObject);
		int type = cmdObject.readByte();
		switch (type) {
		case 100:// 设置房间信息	
			cmdObject.setCmd(Protocol.CMD_SETROOMINFO);
			Log.debug("[GameRoomCmd.CMD_SETROOMINFO]");
			setRoomInfo(cmdObject);
			break;
		case 101://请求玩家的个人概况	
			cmdObject.setCmd(Protocol.CMD_REQUESTINFOMATION);
			sendUserDetail(cmdObject);
			break;
		case 102://下注,通知下一次操作人
			cmdObject.setCmd(Protocol.CMD_OPERATION_NEXT);
			//			GamePool.operationNext();
			break;
		case 103://下注,通知下一次操作人
			cmdObject.setCmd(Protocol.CMD_OPERATION_REQUEST);
			GamePool.CMD_OPERATION_REQUEST(false, cmdObject.getUser().roomId, cmdObject, null);
			break;
		case 104://请求face列表
			cmdObject.setCmd(Protocol.CMD_FACE);
			GamePool.CMD_FACE(cmdObject);
			break;
		case 105://请求今日积宝
			cmdObject.setCmd(Protocol.CMD_TODAYCORNUCOPIALIST);
			GamePool.CMD_TODAYCORNUCOPIALIST(cmdObject);
			break;
		case 106://请求我的积宝
			cmdObject.setCmd(Protocol.CMD_MYCORNUCOPIALIST);
			GamePool.CMD_MYCORNUCOPIALIST(cmdObject);
			break;
		default:
			break;
		}
	}

	private void sendUserDetail(BaseCommandObject cmdObject) {
		int userid = cmdObject.readInt();
		UserInfoEntity user = DaoManager.UserInfoDao(cmdObject.dbIndex).getUserInfo(userid);
		cmdObject.writeBegin();
		if (user != null) {
			cmdObject.writeByte((byte) 0);
			cmdObject.writeString(user.maxHandCards);
			cmdObject.writeInt(user.maxWin);
			cmdObject.writeFloat(user.maxmoney);
			cmdObject.writeInt(user.sumGame);
			cmdObject.writeInt(user.winGame);
			cmdObject.writeFloat(user.money);
			cmdObject.writeInt(user.rank);
		} else {
			cmdObject.writeByte((byte) 1);
		}
		cmdObject.writeEnd();
		UserMgr.I().SendData(cmdObject);
	}

	private void setRoomInfo(BaseCommandObject cmdObject) {
		UserInfoEntity user = cmdObject.getUser();
		int error = 0;
		if (user != null && user.roomId > 0) {
			RoomConfigItem room = RoomMgr.roomsMap.get(user.roomId);
			//检查合法性
			if (room != null && room.roomSeatSize() == 0 && room.canSetRoom.get() == true) {
				String roomname = cmdObject.readString();
				int roomid = cmdObject.readInt();
				if (roomid == user.roomId) {
					room.setName(roomname);
					room.seat = cmdObject.readInt();
					room.minBlind = cmdObject.readFloat();
					room.maxBlind = cmdObject.readFloat();
					room.minTake = cmdObject.readFloat();
					room.maxTake = cmdObject.readFloat();
					Log.debug("[GameRoomCmd.CMD_SETROOMINFO]房间信息设置成功!" + room.toString());
					cmdObject.writeBegin();
					cmdObject.writeByte((byte) 0);
					cmdObject.writeString(room.getName());
					cmdObject.writeInt(room.id);
					cmdObject.writeInt(room.seat);
					cmdObject.writeFloat(room.minBlind);
					cmdObject.writeFloat(room.maxBlind);
					cmdObject.writeFloat(room.minTake);
					cmdObject.writeFloat(room.maxTake);
				} else {
					error = 1;
				}

			} else {
				error = 1;
			}
		} else {
			error = 1;
		}
		if (error != 0) {
			cmdObject.writeBegin();
			cmdObject.writeByte((byte) error);
		}

		cmdObject.writeEnd();
		//		UserMgr.I().broadCastToRoomUsers(cmdObject);
		UserMgr.I().broadCastToAllOnlines(cmdObject);
	}
}
